package com.ciei.dpagm.controller;

import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.controller.BaseController;
import com.ciei.dpagm.entity.Users;
import com.ciei.dpagm.service.DepartmentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class DepartmentController extends BaseController {

	@Autowired
	private DepartmentService departmentService;


	/**
	 * 加载当前登录用户公司的部门数据
	 * @param
	 * @return
	 */
	@RequestMapping("vue/department/loadTree")
	@ResponseBody
	public JSONObject loadTree() {
		Users user = (Users) SecurityUtils.getSubject().getPrincipal();

		return departmentService.loadTreeVue(user.getCompanyId());
	}
	/**
	 * 加载当前登录用户公司的部门数据,不包括公司
	 * @param
	 * @return
	 */
	@RequestMapping("vue/department/loadDepartmentTree")
	@ResponseBody
	public JSONObject loadDepartmentTree() {
		Users user = (Users) SecurityUtils.getSubject().getPrincipal();

		return departmentService.loadDepartmentTree(user.getCompanyId());
	}

	/**
	 * 部门管理-添加部门
	 */
	@RequiresPermissions("system:department:add")
	@RequestMapping("vue/department/add")
	@ResponseBody
	public JSONObject addVue(@RequestBody JSONObject jsonObject) {
		Users user = (Users) SecurityUtils.getSubject().getPrincipal();
		return departmentService.addVue(jsonObject,user.getCompanyId());
	}

	/**
	 * 部门管理-删除
	 */
	@RequiresPermissions("system:department:del")
	@RequestMapping("vue/department/delete")
	@ResponseBody
	public JSONObject deleteVue(@RequestBody JSONObject jsonObject) {

		return departmentService.deleteVue(jsonObject);
	}
	@RequestMapping("vue/department/updateCompanyName")
	@ResponseBody
	public JSONObject updateCompanyNameVue(@RequestBody JSONObject jsonObject) {

		return departmentService.updateCompanyNameVue(jsonObject);
	}

	/**
	 * 部门管理-修改部门
	 */
	@RequiresPermissions("system:department:edit")
	@RequestMapping("department/update")
	@ResponseBody
	public JSONObject update(@RequestBody JSONObject jsonObject) {
		return departmentService.update(jsonObject);
	}

	@RequestMapping("department/init")
	@ResponseBody
	public JSONObject init(@RequestParam("departmentId") Integer departmentId) {
		return departmentService.init(departmentId);
	}

}
